import * as cts from 'amtf/constants';
export function 拼组件名(主名称) {
  let res = cts.组件名前缀 + 转大驼峰(主名称)
  return res
}

export function 拼样式名(组件名, 状态名) {
  let res = `${cts.样式名前缀}-${组件名}`
  if (状态名) {
    res = `${res}-${状态名}`
  }
  // console.log(`res 👉`, res)
  return res
}

function 首字母大写(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}
function 转大驼峰(str) {
  let words = str.split('-');
  return words.map(word => word.charAt(0).toUpperCase() + word.slice(1)).join('');
}

import { getCurrentInstance } from 'vue';

export function getParentCompByName(name) {
  const instance = getCurrentInstance();
  let parent = instance.parent;

  while (parent) {
    if (parent.type.name !== name) {
      parent = parent.parent;
    } else {
      return parent;
    }
  }

  return null;
}

/**
 * Generate a non duplicate ID
 */
export function getRandomId() {
  return Date.now().toString(36) + Math.random().toString(36).substr(2);
}


export function isEmptyArray(arr) {
  return !(Array.isArray(arr) && arr.length > 0);
}


export function isEmptyValue(value) {
  return !(value !== "" && value !== undefined && value !== null);
}


export function isDefined(val) {
  return val !== undefined && val !== null;
}


export function isObject(val) {
  return val !== null && typeof val === "object";
}


export function isFunction(val) {
  return typeof val === "function";
}


export function isBoolean(val) {
  return typeof val === "boolean";
}


export function isNumber(val) {
  return typeof val === "number";
}


export function isTrue(val) {
  return isBoolean(val) && val;
}


export function isFalse(val) {
  return isBoolean(val) && !val;
}


export function getValByUnit(width) {
  return typeof width === "number" ? width + "px" : width;
}


export function getChildCompsByName(context, name) {
  let result = [];

  let childrens = context.$children;

  while (childrens && childrens.length > 0) {
    childrens.forEach((child) => {
      childrens = child.$children ? child.$children : null;

      if (child.$options.name === name) {
        result.push(child);
      }
    });
  }

  return result;
}


export function scrollTo(el, option) {
  if (isFunction(el.scrollTo)) {
    el.scrollTo(option);
  } else {
    const { top, left } = option;
    el.scrollTop = top;
    el.scrollLeft = left;
  }
}
